//
//  orderDetailtestTableViewCell.swift
//  yuejian
//
//  Created by yanziye on 2017/10/27.
//  Copyright © 2017年 gyy. All rights reserved.
//

import UIKit

class orderDetailtestTableViewCell: UITableViewCell {

    var train_id: NSString!
    var train: YJTrainingModel! {
        didSet {
            train_id = train.train_id
        }
    }
    var trainImageView: UIImageView!
    //定义训练名字title
    var trainNameTitle: UILabel!
    //定义训练名字
    var trainName: UILabel!
    //定义训练title
    var priceTitle: UILabel!
    //定义训练
    var price: UILabel!
    //定义推荐指数title
    var recommandTitle: UILabel!
    //定义推荐指数
    var recommand: UILabel!
    
    //定义训练编号title
    var shop_idTitle: UILabel!
    //定义训练编号
    var shop_id: UILabel!
    //购买数量
    var numTitle: UILabel!
    var num: UITextField!
    //总价
    var totalPriceTitle: UILabel!
    var totalPrice: UILabel!
    //当前时间
    var currentTime:String!
    //建议配送时间
    var suggestTime:String!
    //下单时间
    var orderTimeLabel: UILabel!
    //审核时间
    var deliverTimeLabel: UILabel!
    
    //确认btn
    var commitBtn: UIButton!

    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        trainImageView = UIImageView()
        self.contentView.addSubview(trainImageView)
        
        trainNameTitle = UILabel()
        trainNameTitle.text = "训练名称"
        trainNameTitle.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(trainNameTitle)
        
        trainName = UILabel()
        self.contentView.addSubview(trainName)
        
        priceTitle = UILabel()
        priceTitle.text = "培训价格"
        priceTitle.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(priceTitle)
        
        price = UILabel()
        self.contentView.addSubview(price)
        
        recommandTitle = UILabel()
        recommandTitle.text = "推荐指数"
        recommandTitle.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(recommandTitle)
        
        recommand = UILabel()
        self.contentView.addSubview(recommand)
        
        shop_idTitle = UILabel()
        shop_idTitle.text = "培训编号"
        shop_idTitle.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(shop_idTitle)
        
        shop_id = UILabel()
        self.contentView.addSubview(shop_id)
        
        numTitle = UILabel()
        numTitle.text = "培训人数"
        numTitle.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(numTitle)
        
        num = UITextField()
        
        num.backgroundColor = UIColor.lightGray
        num.font = UIFont.systemFont(ofSize: 18)
        num.text = "0"
        num.keyboardType = .decimalPad
        
        NotificationCenter.default.addObserver(self, selector: #selector(textfieldDidChange), name: .UITextFieldTextDidChange, object: num)
        
        self.contentView.addSubview(num)
        
        totalPriceTitle = UILabel()
        totalPriceTitle.text = "总价"
        totalPriceTitle.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(totalPriceTitle)
        
        totalPrice = UILabel()
        totalPrice.font = UIFont.systemFont(ofSize: 18)
        totalPrice.text = "0"
        self.contentView.addSubview(totalPrice)
        
        orderTimeLabel = UILabel()
        orderTimeLabel.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(orderTimeLabel)
        
        deliverTimeLabel = UILabel()
        deliverTimeLabel.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(deliverTimeLabel)
        
        let nowDate = NSDate()
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"
        currentTime = formatter.string(from: nowDate as Date)
        var timeInterval:TimeInterval = nowDate.timeIntervalSince1970
        let timeStamp = Int(timeInterval) + 1800
        timeInterval = TimeInterval(timeStamp)
        let date = NSDate(timeIntervalSince1970: timeInterval)
        suggestTime = formatter.string(from: date as Date)
        orderTimeLabel.text = "报名时间:\(currentTime!)"
        deliverTimeLabel.text = "审核时间:\(currentTime!)-\(suggestTime!)"
        
        let lastLine = CAShapeLayer()
        lastLine.path = UIBezierPath(rect: CGRect(x: 0, y: 230, width: ScreenWidth, height: 2)).cgPath
        lastLine.fillColor = UIColor.gray.cgColor
        self.contentView.layer.addSublayer(lastLine)
        
        commitBtn = UIButton()
        commitBtn.backgroundColor = UIColor.red
        commitBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
        commitBtn.setTitle("确认报名", for: UIControlState.normal)
        commitBtn.addTarget(self, action:#selector(commit) , for: UIControlEvents.touchUpInside)
        self.contentView.addSubview(commitBtn)
       
    }
    
    
    func textfieldDidChange(){
        if num.text?.characters.count != 0 {
            let total = Float(num.text!)!*Float(price.text!)!
            totalPrice.text = "\(total)"
        }else{
            totalPrice.text = "0"
        }
    }
    
    @objc func commit(sender:UIButton){
        sender.isEnabled = false
        let parameter:NSDictionary = [
            "userid":train.userid,
            "train_id":train.train_id,
            "num":Int(num.text!)!,
            "sum":Double(totalPrice.text!)!,
            "suggesttime":"\(currentTime)-\(suggestTime)"
        ]
        YJNetworkMngTool.sharedNetWorkMngTool().YJNetwork_insertOrderWithParameters(parameter: parameter) {
            DispatchQueue.main.async {
                let popup = Popup()
                popup.appear(title: "报名成功", action: {
                    //   self.back()
                })
            }
        }
    }
    
    
    
    //2.设置控件位置
    override func layoutSubviews() {
        super.layoutSubviews()
        trainImageView.frame = CGRect(x: 0, y: 0, width: CGFloat(WHRatio * CELLHEIGHT * 2.2), height: CELLHEIGHT * 2.3);
        
        trainNameTitle.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 10, width: 100, height: 30);
        
        trainName.frame = CGRect( x: 100, y: CELLHEIGHT * 2.3 + 10,width: ScreenWidth-100, height: 30);
        
        priceTitle.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 40, width: 100, height: 30);
        
        price.frame = CGRect(x: 100, y: CELLHEIGHT * 2.3 + 40,width: ScreenWidth-100, height: 30);
        
        recommandTitle.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 70, width: 100, height: 30);
        
        recommand.frame = CGRect(x: 100, y: CELLHEIGHT * 2.3 + 70, width: ScreenWidth-100, height: 30);
        
        shop_idTitle.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 100, width: 100, height: 30);
        
        shop_id.frame = CGRect(x: 100, y: CELLHEIGHT * 2.3 + 100, width: ScreenWidth-100, height: 30);
        
        numTitle.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 130, width: 100, height: 30);
        num.frame = CGRect(x: 100, y: CELLHEIGHT * 2.3 + 130, width: 150, height: 30);
        
        totalPriceTitle.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 160, width: 100, height: 30);
        totalPrice.frame = CGRect(x: 100, y: CELLHEIGHT * 2.3 + 160, width: ScreenWidth-100, height: 30);
        
        orderTimeLabel.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 190, width: 230, height: 30);
        
        deliverTimeLabel.frame = CGRect(x: 5, y: CELLHEIGHT * 2.3 + 220, width: 230, height: 30);
        
        commitBtn.frame = CGRect(x: 70, y: CELLHEIGHT * 2.3 + 280, width: 275, height: 45);
        
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}
